/*
 * SearchWindowController.java
 *
 * Created on November 12, 2006, 8:45 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.search;

import edu.umn.cs5115.scheduler.SchedulerDocument;
import edu.umn.cs5115.scheduler.framework.WindowController;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

/**
 * Window controller for the search/search results windows.
 * @author grant
 */
public class SearchWindowController extends WindowController
{
    /** Created the search interface */
    private SearchDialog searchInterface;
    /** Actual interface created by the SearchDialog class. */
    private Composite searchPanel;
    /** Created the search results interface */
    private SearchResultsDisplay searchResultsDisplay;
    /** Actual interface created by the SearchResultsDisplay. */
    private Composite resultsPanel;
    
    /** Creates a new instance of SearchWindowController */
    public SearchWindowController(SchedulerDocument document)
    {
        super(document);
        
        GridLayout layout = new GridLayout();
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        
        Shell window = new Shell();
        window.setLayout(layout);
        
        setWindow(window);
        
        showSearchInterface();
        
        window.pack();
        window.addListener(SWT.Resize, new Listener()
        {
            public void handleEvent(Event event)
            {
                relayoutContent();
            }
        });
    }
    
    /** 
     * Window controller checks the return value of this method to get the
     * window's title.
     * @return The string "Search -- <document name>"
     */
    public String getWindowTitle()
    {
        // \u2014 is an em dash.
        return "Search \u2014 " + getDocument().getDisplayName();
    }

    
    public void showSearchResults(SearchResults results, boolean showBackButton)
    {
    	// hide search panel
    	if (searchPanel != null) {
	    	searchPanel.setVisible(false);
	    	((GridData)searchPanel.getLayoutData()).exclude = true;
    	}
    	
    	// create results panel
    	// TODO: this is inefficient--the whole results panel does not need to be re-created
    	searchResultsDisplay = new SearchResultsDisplay(this, results, showBackButton);
        resultsPanel = searchResultsDisplay.createSearchResultsInterface();
        resultsPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        
        relayoutContent();
    }

    public void showSearchInterface()
    {
    	// hide results panel
    	if ( resultsPanel != null ) {
	    	resultsPanel.setVisible(false);
	    	((GridData)resultsPanel.getLayoutData()).exclude = true;
    	}
    	
    	// activate search panel
    	if ( searchPanel == null ) {
          searchInterface = new SearchDialog(this);
          searchPanel = searchInterface.createPanel();
          searchPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    	}
    	searchPanel.setVisible(true);
    	((GridData)searchPanel.getLayoutData()).exclude = false;
    	
        relayoutContent();
    }
    
    /**
     * Stretches panels to the size of the window.
     * Updates the layout of the window (.layout(true));
     */
    private void relayoutContent()
    {
        this.getWindow().layout(true);
    }
}
